package com.ss.ttm.player;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class SensorData implements SensorEventListener {
    protected static final int Sensor_ACC_Data = 1;
    protected static final int Sensor_MAG_Data = 2;
    private float[] accel;
    private long mHandle;
    private SensorManager mSensorManager;
    private float[] magnet;

    public SensorData() {

        this.mHandle = 0;
        this.mSensorManager = null;
        this.magnet = new float[3];
        this.accel = new float[3];
    }

    private static final native void _writeData(long arg0, int arg1, float arg2, float arg3, float arg4);

    protected void finalize() {
        this.stop();
    }

    public void initListeners() {
        this.mSensorManager.registerListener(((SensorEventListener) this), this.mSensorManager.getDefaultSensor(1), 0);
        this.mSensorManager.registerListener(((SensorEventListener) this), this.mSensorManager.getDefaultSensor(4), 0);
        this.mSensorManager.registerListener(((SensorEventListener) this), this.mSensorManager.getDefaultSensor(2), 0);
    }

    public void onAccuracyChanged(Sensor arg1, int arg2) {
    }

    public void onSensorChanged(SensorEvent arg8) {
        int v5 = 3;
        float v3 = 0.001f;
        int v6 = 2;
        switch (arg8.sensor.getType()) {
            case 1: {
                System.arraycopy(arg8.values, 0, this.accel, 0, v5);
                if (this.accel[0] < v3 && this.accel[1] < v3 && this.accel[v6] < v3) {
                    return;
                }

                SensorData._writeData(this.mHandle, 1, this.accel[0], this.accel[1], this.accel[v6]);
                break;
            }
            case 2: {
                System.arraycopy(arg8.values, 0, this.magnet, 0, v5);
                SensorData._writeData(this.mHandle, v6, this.magnet[0], this.magnet[1], this.magnet[v6]);
                break;
            }
        }
    }

    public void pause() {
        if (this.mSensorManager != null) {
            this.mSensorManager.unregisterListener(((SensorEventListener) this));
        }
    }

    public void resume() {
        this.initListeners();
    }

    public void setHandle(long arg4, TTPlayer arg6) {
        this.mHandle = arg4;
        this.mSensorManager = (SensorManager) arg6.getContext().getSystemService(Context.SENSOR_SERVICE);
        this.initListeners();
    }

    public void stop() {
        if (this.mSensorManager != null) {
            this.mSensorManager.unregisterListener(((SensorEventListener) this));
            this.mSensorManager = null;
        }
    }
}

